<template>
	<div class="login">
		<router-link :to="{name:'Home'}"><img class="logo" src="../assets/images/logo.jpg" alt=""></router-link>
		<van-form @submit="onSubmit">
			<van-field v-model="userName" name="userName" label="用户名" placeholder="请填写用户名"  />
			<van-field v-model="password" type="password" name="password" label="密码" placeholder="请填写密码"  />
            <van-field v-model="rePassword" type="password" label="确认密码" placeholder="再一次输入密码"/>
			<div style="margin: 16px;"><van-button round block type="info" native-type="submit">注册</van-button></div>
		</van-form>
		<router-link style="float:right;margin:16px" :to="{name:'Login'}">已有账号，我要登录</router-link>
	</div>
</template>

<script>
import {Toast} from "vant"
import {regAPI} from "../services/auth"
import {setToken} from "../utils/tools"
export default {
	name: 'Reg',
	data() {
		return {
			userName: '',
			password: '',
            rePassword:''
		};
	},
	methods: {
		async onSubmit(values) {
			const resLogin=await regAPI(values);
            if(this.password==this.rePassword){
                    if(resLogin.code===1){
                    setToken(resLogin.data)
                    Toast.success("注册成功")
					//登录成功获取购车数量
					this.$eventBus.$emit("updateCartCount")
                    this.$router.push({name:'User'})
                }else{
                    Toast.fail(resLogin.data)
                }
            }
			
		}
	}
};
</script>

<style scoped="scoped" lang="scss">
</style>
